import { AdapterMap } from './map'

const TransAdapter = {
  install(flow) {
    flow.adapterIn = this.adapterIn
    flow.adapterOut = this.adapterOut
  },
  adapterIn(data) {
    return data
  },
  adapterOut(bpmnData) {
    debugger
    console.group('导出数据')
    let xml = ''
    xml += '<transformation>'

    console.log('处理数据源连接', bpmnData)
    const connection = '<connection></connection>'
    xml += connection

    const edges = bpmnData.edges
    console.log('遍历连线', edges)
    let order = '<order><hop>'
    for (let i = 0; i < edges.length; i++) {
      order += `<form>${edges[i]}</form>`
    }
    order += '</hop></order>'
    xml += order

    const nodes = bpmnData.nodes
    console.log('遍历节点', nodes)
    for (let i = 0; i < nodes.length; i++) {
      console.log('节点类型', nodes[i].type)
      const nodeXml = AdapterMap[nodes[i].type](nodes[i].properties)
      console.log('转换节点数据为', nodeXml)
      xml += nodeXml
    }
    console.groupEnd()
    return xml
  }
}

export { TransAdapter }
